package com.hornsun.client.discover.api;

import com.hornsun.client.discover.dto.DiscoverDTO;
import com.hornsun.client.discover.dto.MoreInformationDTO;
import com.hornsun.client.discover.information.service.InformationService;
import com.hornsun.domain.constant.types.InfoType;
import com.hornsun.domain.dbo.ContentType;
import com.hornsun.repository.ContentTypeRepository;
import com.hornsun.repository.InformationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 编译中... on 2018/4/13.
 */
@RestController
@RequestMapping("/api/discover")
public class DiscoverAPI {
    @Autowired
    private ContentTypeRepository contentTypeRepository;
    @Autowired
    private InformationService informationService;
    @Resource
    private InformationRepository informationRepository;

    /**
     * 进入资讯页
     * @return
     */
    @GetMapping("/getDiscoverInfo")
    public ResponseEntity<DiscoverDTO> getDiscoverInfo(){
        List<ContentType> contentTypeList =contentTypeRepository.queryContentType(InfoType.INFORMATION.getType());
        if (contentTypeList.isEmpty()||contentTypeList.size()==0){
            return new ResponseEntity<DiscoverDTO>(HttpStatus.OK);
        }
        DiscoverDTO discoverDTO = new DiscoverDTO();
        discoverDTO.setTypes(contentTypeList);
        //进入“发现”页面时，显示第一种类型的第一页
        ContentType contentType = contentTypeList.get(0);
        int page = 1;
        discoverDTO.setInformationDTO(informationService.getInformationByType(contentType.getId(),page));
        discoverDTO.setTotalCount(informationRepository.getCountByTypeid(contentType.getId()));
        return new ResponseEntity<DiscoverDTO>(discoverDTO, HttpStatus.OK);
    }

    /**
     * 获取更多资讯
     * @param typeId
     * @param page
     * @return
     */
    @GetMapping("/getMoreInformation")
    public ResponseEntity<MoreInformationDTO> getMoreInformation(@RequestParam("typeid")String typeId , @RequestParam("page")Integer page){
        MoreInformationDTO moreInformationDTO = new MoreInformationDTO();
        moreInformationDTO.setInformationList(informationService.getInformationByType(typeId,page));
        moreInformationDTO.setTotalCount(informationRepository.getCountByTypeid(typeId));
        return new ResponseEntity<MoreInformationDTO>(moreInformationDTO,HttpStatus.OK);
    }

    /**
     * 点赞
     * @param informationGuid
     * @return
     */
    @Transactional
    @PostMapping("/clickPraise")
    public ResponseEntity<?> clickPraise(@RequestParam("guid")String informationGuid){
        informationRepository.clickPraise(informationGuid);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 点击量
     * @param informationGuid
     * @return
     */
    @Transactional
    @PostMapping("/click")
    public ResponseEntity<?> click(@RequestParam("guid") String informationGuid){
        informationRepository.click(informationGuid);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }
}
